﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLiNhaSachBUS;
using QuanLiNhaSachDTO;
namespace QuanLiNhaSachGUI
{
    public partial class frmQuanLiNhanVien : Form
    {
        public frmQuanLiNhanVien()
        {
            InitializeComponent();
            dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
            checkButton();
        }


        void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                txtID.Text = dataGridView1[0, e.RowIndex].Value.ToString();
                txtHoTen.Text = dataGridView1[1, e.RowIndex].Value.ToString();
                dateTimePicker1.Text = dataGridView1[2, e.RowIndex].Value.ToString();
                txtCMND.Text = dataGridView1[3, e.RowIndex].Value.ToString();
                cbGioiTinh.Text = dataGridView1[4, e.RowIndex].Value.ToString();
                txtSoDienThoai.Text = dataGridView1[5, e.RowIndex].Value.ToString();
                txtDiaChi.Text = dataGridView1[6, e.RowIndex].Value.ToString();
                txtEmail.Text = dataGridView1[7, e.RowIndex].Value.ToString();
                txtUsername.Text = dataGridView1[8, e.RowIndex].Value.ToString();
                txtPassword.Text = dataGridView1[9, e.RowIndex].Value.ToString();
                cbLoai.Text = dataGridView1[10, e.RowIndex].Value.ToString();
            }
        }

        void loadAll()
        {
            dataGridView1.DataSource = NhanVienBUS.loadAll();
            dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            NhanVien nv = new NhanVien();
            nv.CMND = txtCMND.Text;
            nv.Ten = txtHoTen.Text;
            nv.NgaySinh = dateTimePicker1.Value;
            nv.GioiTinh = cbGioiTinh.Text;
            nv.SoDienThoai = txtSoDienThoai.Text;
            nv.DiaChi = txtDiaChi.Text;
            nv.Email = txtEmail.Text;
            nv.Username = txtUsername.Text;
            nv.Password = txtPassword.Text;
            nv.Loai = cbLoai.Text;

            NhanVienBUS.insert(nv);
            loadAll();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            NhanVienBUS.delete(txtID.Text);
            loadAll();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            NhanVien nv = new NhanVien();
            nv.CMND = txtCMND.Text;
            nv.Ten = txtHoTen.Text;
            nv.NgaySinh = dateTimePicker1.Value;
            nv.GioiTinh = cbGioiTinh.Text;
            nv.SoDienThoai = txtSoDienThoai.Text;
            nv.DiaChi = txtDiaChi.Text;
            nv.Email = txtEmail.Text;
            nv.Username = txtUsername.Text;
            nv.Password = txtPassword.Text;
            nv.Loai = cbLoai.Text;
            nv.id = txtID.Text;

            if (nv.id == "1")
                nv.Loai = "admin";
            NhanVienBUS.update(nv);
       
            loadAll();
            
        }

        private void txtCMND_TextChanged(object sender, EventArgs e)
        {
            
            string tmpStr="";
            for (int i=0; i<txtCMND.Text.Length; i++)         
                if (txtCMND.Text[i]>='0' && txtCMND.Text[i]<='9')
                    tmpStr+=txtCMND.Text[i];
            if (txtCMND.Text == tmpStr) return;
            txtCMND.Text = tmpStr;
            txtCMND.Select(txtCMND.Text.Length, 0);
        }

        private void btnNhapMoi_Click(object sender, EventArgs e)
        {
            
            txtID.Text = "";
            txtHoTen.Text = "";
            dateTimePicker1.Text = "";
            txtCMND.Text = "";
            cbGioiTinh.Text = "";
            txtSoDienThoai.Text = "";
            txtDiaChi.Text = "";
            txtEmail.Text = "";
            txtUsername.Text = "";
            txtPassword.Text = "";
            cbLoai.Text = "";
        }

        private void txtHoTen_TextChanged(object sender, EventArgs e)
        {
            checkButton();
        }

        void checkButton()
        {
            if (txtHoTen.Text == "")
            {
                btnLuu.Enabled = false;
                btnCapNhat.Enabled = false;
                if (txtID.Text != "")
                {
                    btnXoa.Enabled = true;
                }
                else btnXoa.Enabled = false;
            }
            if (txtHoTen.Text != "")
            {
                if (txtID.Text != "")
                {
                    btnXoa.Enabled = true;
                    btnCapNhat.Enabled = true;
                    btnLuu.Enabled = false;
                }
                else
                {
                    btnXoa.Enabled = false;
                    btnCapNhat.Enabled = false;
                    btnLuu.Enabled = true;
                }
            }
            
            
        }

        private void txtID_TextChanged(object sender, EventArgs e)
        {
            checkButton();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void frmQuanLiNhanVien_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmMain.listForm.Remove(this);
        }

        private void frmQuanLiNhanVien_Load(object sender, EventArgs e)
        {
            loadAll();
        }
    }
}
